package com.zhou.listener;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zhou.pojo.FlightInfo;
import com.zhou.pojo.FlightPathDto;
import com.zhou.util.JsonUtil;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class RedisChanelListener implements MessageListener {
 
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisPublish redisPublish;
 
    @Override
    public void onMessage(Message message, byte[] pattern) {
        // 获取消息
        byte[] messageBody = message.getBody();
        // 使用值序列化器转换
        Object msg = redisTemplate.getStringSerializer().deserialize(messageBody);
        // 获取监听的频道
        byte[] channelByte = message.getChannel();
        // 使用字符串序列化器转换
        Object channel = redisTemplate.getStringSerializer().deserialize(channelByte);
        // 渠道名称转换
        String patternStr = new String(pattern);
        System.out.println(patternStr);
        System.out.println("---频道---: " + channel);
        System.out.println("---消息内容---: " + msg);

        try {
            String msgString = msg.toString();
            doConvert(msgString);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private void doConvert(String msgString) throws Exception {
        JSONObject jsonObject = JSONUtil.parseObj(msgString);


        FlightPathDto flightPathDto = new FlightPathDto();
        flightPathDto.setIcao_code(jsonObject.get("type",String.class));
        JSONArray trace = jsonObject.getJSONArray("trace");

        //时间戳判断
        if(String.valueOf(jsonObject.get("timestamp",long.class)).length()!=13){
            Date date = new Date(((jsonObject.get("timestamp",long.class)+trace.get(0, Date.class).getTime())*1000));
            flightPathDto.setTimestamp(date);
        }else {
            Date date = new Date((jsonObject.get("timestamp",long.class)+trace.get(0, Date.class).getTime()));
            flightPathDto.setTimestamp(date);
        }


        flightPathDto.setLatitude(trace.get(1, double.class));
        flightPathDto.setLongitude(trace.get(2, double.class));

        //位置高度判断
        if(trace.get(3).getClass().equals(String.class)||trace.get(3).getClass()==null){
            flightPathDto.setSpeed(0.0);
        }else {
            flightPathDto.setSpeed(trace.get(3, double.class));
        }

        flightPathDto.setAltitude(trace.get(4, double.class));
        flightPathDto.setHeading(trace.get(5, double.class));

        //状态判断
        if(trace.get(8, String.class)==null){
            flightPathDto.setSource_type("");
        }else {
            flightPathDto.setSource_type(trace.get(8, String.class));
        }

        System.out.println(flightPathDto.toString());
        String serialize = JsonUtil.serialize(flightPathDto);

        redisPublish.pushPATH(serialize);
    }
}